<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

header('Content-Type: text/plain');

$q = json_decode(MiscUtils::getParam('q', NULL));

  foreach ($q as &$tmp) {
    $tablename = $tmp->n;
    if ($tablename) {
        if(is_array($tmp->a))
        {
            foreach ($tmp->a as &$obj) 
            {
                /*if (isset($obj->e_oid) || isset($obj->id)) 
                {
                  pdoupdate($obj,$tablename,true);
                } else {
                  pdoadd($obj,$tablename,true);
                }*/
            	pdoupdate($obj,$tablename);
            }           
            
        }

      
      if(is_array($tmp->r))
      {
         foreach ($tmp->r as &$obj)
         {
            pdoremove($obj,$tablename);
         }          
      }

    }
  }

  /****************************************************数据操作**************************************/
/**
 *
 * pdoupdate 
 */    
 function pdoupdate($obj,$tablename,$debug='')
 {
 	switch($tablename)
 	{
 		case'v2purchaseitem':
 			$tname = 'v1purchaseitem';
 			break;

 		case'v2purchaseitemselect':
 			$tname = 'v1purchaseitemselect';
 			break;	

 		default:
 			$tname = $tablename;
 			break;	
 		
 	}
 	
 	
    if(!empty($debug))
    {
        print_r($obj);
    }
    
    if($tablename == 'v2purchaseitem')
    {//只对v2purchaseitem操作，防止重复操作
		    if (!empty($obj))
		    {
		        
		        $producefield = "update ns_".$tname." set ";
		        
		        foreach ($obj as $key => $value)
		        {
		            if(!in_array($key,array('_added','notes','selects','v2_events','v2_tracks','v2_produces',
		                                    'v2_notes','id','e_oid','_text','when','deadline','_method','_exist')))
		            {
		                if(in_array($key,array('deadline','date','createdDate','sentDate','receivedDate')))
		                {
		                    if(is_object($obj->$key))
		                    {
		                        $value = SimpleDate::toStamp($value);
		                        if(empty($value)) $value =0;
		                        
		                    }else{
		                    	if(empty($obj->$key)) $value = 0;
		                    }
		                    
		                }
		                
		                $producefield .= '`'.$key."`='".addslashes($value)."',";
		            }
		            
		
		           if(in_array($key,array('when','deadline')))
		           {
		               if(empty($obj->$key)) 
		               {
		                  $obj->$key = 0;
		                  $producefield .= " `$key` = '".$obj->$key."',";
		               }else{
		                  $producefield .= " `$key` = '".SimpleDate::toStamp($obj->$key)."',";
		               }
		                    
		               
		           }                    
		            
		        }
		        
		        $producefield = substr($producefield,0,-1);
		        $producefield .=" where e_oid = '".$obj->e_oid."'";
		        
		        if(!empty($debug))
		        {
		            echo $producefield."\n";
		        }else{
		            sql_execute($producefield);
		        }
		        
		        unset($key);
		        unset($value);
		        
		        if(!empty($obj->selects))
		        {
		        	$nodeleteid = '';
		        	foreach($obj->selects as $purselect1)
		        	{
		        		if(isset($purselect1->e_oid) && isset($purselect1->id))
		        		{
		        			$nodeleteid .= $purselect1->e_oid.',';
		        		}
		        	}
		        	
		        	
		        	    if(empty($nodeleteid))
                        {
                            $deletepurchaseselect = "delete from ns_v1purchaseitemselect where purchaseitem_id = '".$obj->e_oid."'";//先删除后增加
                            if(!empty($debug))
                            {
                                echo $deletepurchaseselect."\n";
                            }else{
                                sql_execute($deletepurchaseselect);
                            }
                            
                        }else{
                            $nodeleteid = substr($nodeleteid,0,-1);
                            $deletepurchaseselect = "delete from ns_v1purchaseitemselect where purchaseitem_id = '".$obj->e_oid."' and e_oid not in ($nodeleteid)";//先删除后增加
                            if(!empty($debug))
                            {
                                echo $deletepurchaseselect."\n";
                            }else{
                                sql_execute($deletepurchaseselect);
                            }                            
                            
                        }  

                            
                            
		        	foreach($obj->selects as $purselect)
		        	{
		        		if( isset($purselect->e_oid) || isset($purselect->id) )
		        		{
		        			$purchaseitemusql = "update ns_v1purchaseitemselect set ";
		        			foreach ($purselect as $key => $value)
		        			{
		        				if(!in_array($key,array('id','e_oid')))
		        				{
		        					$purchaseitemusql .= '`'.$key."`='".addslashes($value)."',";
		        				}
		        			}
		        			
					        $purchaseitemusql = substr($purchaseitemusql,0,-1);
					        $purchaseitemusql .=" where e_oid = '".$purselect->e_oid."'";   
		
					        if(!empty($debug))
					        {
					        	echo $purchaseitemusql."\n";
					        }else{
					        	sql_execute($purchaseitemusql);
					        }
					        unset($key);
					        unset($value);
		        			
		        		}else{
		        			
		        			$purselectfield = '';
		                    $purselectvalue = '';
		            
		        			foreach ($purselect as $key => $value)
		        			{
		        				if(!in_array($key,array('purchaseitem_id')))
		        				{
		        					$purselectfield .='`'.$key.'`,';
		        					$purselectvalue .= " '".addslashes($value)."',";
		        				}
		        				 
		        			}
		        			
				            $purselectfield .= '`purchaseitem_id`';
				            $purselectvalue .= "'".$obj->e_oid."'";
				
				            $purchaseitemusql = "insert into ns_v1purchaseitemselect(`e_oid`,".$purselectfield.") values(NULL,".$purselectvalue.")";  
				             
				            if(!empty($debug))
				            {
				                echo $purchaseitemusql."\n";         
				            }else{
				                sql_execute($purchaseitemusql);
				            } 
				            
				            unset($key);
				            unset($value);        			
		        			
		        		}//add end
		        		
		        	}//one select
		        	
		        }//select not null
		
		        
		    }//obj not null
    }
    
 }//end pdoupdate

 
 /**
  * 
  * pdoadd
  */
 function pdoadd($obj,$tablename,$debug='')
 {
 	/*
  	switch($tablename)
 	{
 		case'v2purchaseitemselect':
 			$tname = 'v1purchaseitemselect';
 			break;	

 		default:
 			$tname = $tablename;
 			break;	
 		
 	} 
 	 	
    if(!empty($debug))
    {
        echo"\n";
        echo"*********************以下为增加操作!**tablename:".$tname."***************************";
        echo"\n";
        print_r($obj);
    }
    
   
    
    if(!empty($obj))
    {
            $producefield = '';
            $producevalue = '';
            
            foreach($obj as $key=>$value)
            {
                if($tname == 'produceuser')
                {
                    if(in_array($key,array('trackId','account','password','name','email','type','produce_id')))
                    {
                        $producefield .='`'.$key.'`,';
    
                        if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date'))) $value = SimpleDate::toStamp($value);
                            
                        $producevalue .= " '".addslashes($value)."',";                      
                    
                    }               
                    
                }else{
                    if(!in_array($key,array('pi_selects','v2_pUsers','v2_purchaseItems','v2_events','v2_tracks','v2_produces','v2_notes','_exist','when','isExpired')))
                    {
                        $producefield .='`'.$key.'`,';
    
                        if(in_array($key,array('deadline','createdDate','sentDate','receivedDate','date')))
                        {
                            if(is_object($obj->$key))
                            {
                                $value = SimpleDate::toStamp($value);
                            }
                            
                        }
                            
                        $producevalue .= " '".addslashes($value)."',";                      
                    
                    }
                }

                if(in_array($key,array('when','isExpired')))
                {
                    $producefield .='`'.$key.'`,'; 
                    
                    if(empty($obj->$key))
                    {
                        $obj->$key = 0;
                        $producevalue .= " '".$obj->$key."',";
                        
                    }else{
                        if($key == 'when'){
                            $producevalue .= " '".SimpleDate::toStamp($obj->$key)."',";
                        }else{
                            $producevalue .= " '".$obj->$key."',";
                        }
                        
                    }
                    
                }               
            
            
            }

            $producefield = substr($producefield,0,-1);
            $producevalue = substr($producevalue,0,-1);

            $producesql = "insert into ns_".$tname."(`e_oid`,".$producefield.") values(NULL,".$producevalue.")";  
             
            if(!empty($debug))
            {
                echo $producesql."\n";         
            }else{
                sql_execute($producesql);
            } 
            
            unset($key);
            unset($value);
    	
        
    }//obj not null
    
    
    */
 }
 
 /**
  * pdoremove
  */
 
   function pdoremove($obj,$tablename,$debug='')
   {
      if(!empty($debug))
      {
        echo "\n";
        echo "***********************tablename:".$tablename."**************************";
        echo "\n";
        print_r($obj);
      }
      
      if(!empty($obj))
      {
        switch($tablename)
        {
            case'producetrack':
                {
                    $deltracksql = "delete from ns_".$tablename." where trackId = '".$obj->trackId."'";
                    
                    if(!empty($debug))
                    {
                        echo $deltracksql."\n";
                    }else{
                        sql_execute($deltracksql);
                    }
                    
                    $deltrackusersql = "delete from ns_producetrackuser where track_trackId = '".$obj->trackId."'";
                    
                    if(!empty($debug))
                    {
                        echo $deltrackusersql."\n";
                    }else{
                        sql_execute($deltrackusersql);
                    }
                    
                }
                break;
            default:
                {
                    $sqldel = "delete from ns_".$tablename." where e_oid ='".$obj->e_oid."'";
                    
                    if(!empty($debug))
                    {
                        echo $sqldel."\n";
                    }else{
                        sql_execute($sqldel);
                    }
                    
                    
                }   
                break;
        }
      }
      
      
    
    
    
    
   }
 
 
 
   

?>